# -*- coding: utf-8 -*-
'''
     @Author : LiWenqian
     @File : 游戏_回合.py
     @Project: scpythonProject
     @CreateTime : 2022/6/18 17:45:51
'''
import random
# 创建用户类
class User(object):
    def __init__(self, name, sex, cost=10, blood=100):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.cost = cost
        # equip为0，没有防弹衣；equip为1，有防弹衣
        self.equip = random.randint(0, 1)
        # 如果有防弹衣，则被攻击时只扣5滴血
        if self.equip == 1:
            self.cost = 5
            print(f"{self.name}获得防弹衣，被攻击时只扣5滴血！")

    def fight(self, user2, room):
        print("="*48)
        print(f"{self.name}的初始血量为{self.blood}   {user2.name}的初始血量为{user2.blood}")
        if (self.name in room._user_instance) and (user2.name in room._user_instance):
            print(f"{self.name}和{user2.name}开始战斗")
            i = 1
            while self.blood > 0 and user2.blood > 0:
                print("*"*20, f"第{i}回合", "*"*20)
                # 随机指定被攻击的一方,1为self.name被攻击，2为user2.name被攻击
                attacked_user = random.randint(1, 2)
                if attacked_user == 1:
                    self.blood -= self.cost
                    print(f"{self.name}被攻击，-{self.cost}滴血")
                else:
                    user2.blood -= user2.cost
                    print(f"{user2.name}被攻击，-{user2.cost}滴血")
                print(f"{self.name}剩余血量：{self.blood}")
                print(f"{user2.name}剩余血量：{user2.blood}")
                i += 1

            if self.blood < user2.blood:
                print(f"{user2.name}获胜！！！")
            else:
                print(f"{self.name}获胜！！！")

        else:
            raise TypeError(f"{self.name}和{user2.name}不在同一个房间，不能进行战斗！")

# 创建房间
class Room(object):
    _user_instance = []
    def __init__(self):
        print("房间创建成功！")

    def user_join(self, role):
        if len(self._user_instance) < 2:
            self._user_instance.append(role.name)
            print(f"{role.name}加入房间")
        else:
            raise ValueError("该房间人数已满，请加入其他房间！")

# if __file__ == '__main__':
user1 = User('lwq', 'w')
user2 = User('lzy', 'w')
user3 = User('wh', 'w')
room2 = Room()
room2.user_join(user1)
room2.user_join(user2)
# room2.user_join(user3)
user1.fight(user2, room2)

